package personal.zq.practice.common.utils.leetcode.medium;

import java.util.Scanner;

/**
 * 求中位数
 *
 * @author zhangqing
 * @version 1.0
 * @date 2022/6/21
 */
class Median {
    public static void main(String[] args) {
        //        Scanner in = new Scanner(System.in);
        //        int[] a = new int[2];
        //        int i = 0;
        //        while (in.hasNextInt() && !in.hasNextLine()) {
        //            a[i] = in.nextInt();
        //            i++;
        //        }
        //        int[] b = new int[2];
        //        int j = 0;
        //        while (in.hasNextInt() && !in.hasNextLine()) {
        //            b[j] = in.nextInt();
        //            j++;
        System.out.println(findMedianSortedArrays(new int[]{1, 3}, new int[]{2}) + "");
        System.out.println(findMedianSortedArrays(new int[]{1, 2}, new int[]{3, 4}) + "");

    }

    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int l = nums1.length + nums2.length;
        int a[] = new int[l];
        int i = 0, j = 0;
        for (int k = 0; k < l; k++) {
            if (i == nums1.length || (i < nums1.length && j < nums2.length && nums1[i] > nums2[j])) {
                a[k] = nums2[j];
                j++;
            } else {
                a[k] = nums1[i];
                i++;
            }
            System.out.println(a[k]);
        }
        if (l % 2 == 0) {
            return (a[(l / 2) - 1] + a[l / 2]) / 2.0;
        } else {
            return a[l / 2];
        }
    }
}
